﻿using CJAtech.BBS.DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bigger.Web.Mvc;
using Bigger.Framework;
using System.Web.Mvc;
using CJAtech.BBS.Web.Models;
using CJAtech.BBS.Process;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Web.Filters
{
    [Obsolete("兼容旧版的微信，新版本请使用WXAuthFilter代替")]
    public class WXAuthorFilter : BiggerActionFilter
    {
        /// <summary>
        /// 权限类型
        /// </summary>
        public PowerType PowerType { get; set; }
        public WXAuthorFilter(PowerType powerType)
        {
            this.PowerType = powerType;
            this.Order = 0;
        }
        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            if (this.PowerType == PowerType.None)
            {
                return;
            }

            var objCommunityConfig = filterContext.HttpContext.GetItems("CommunityConfig") as CommunityConfig;
            string wxToken = CookieHelper.GetCookieValue(objCommunityConfig.ConfigCode + "WXToken");
            UserInfo objUserInfo = null;
            if (wxToken.IsNoNull())
            {
                objUserInfo = wxToken.JsonDeserialize<UserInfo>();
            }
            if (objUserInfo == null)
            {
                if (SystemWebAsset.IsAjaxRequest())
                {
                    filterContext.Result = new JsonResult
                    {
                        Data = new InvokeResult { ResultCode = "NotLogin", ResultMessage = "对不起，请先登录" },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                    return;
                }
                filterContext.Result = new ContentResult
                {
                    Content = "对不起，请先登录"
                };
            }
            else
            {
                filterContext.Controller.ViewBag.UserInfo = objUserInfo;
            }
        }
    }
}